home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C & C++ Multimedia Cyber Classroom
/
C and C++ Multimedia Cyber Classroom (Prentice Hall) (1998).iso
/
cpphtp2
/
code.jar
/
code
/
ch15
/
fig15_12.txt
< prev
next >
Wrap
Text File
|
1998-02-27
|
2KB
|
63 lines
1 // Fig. 15.12: queue.h
2 // Queue class template definition
3 // Derived from class List
4 #ifndef QUEUE_H
5 #define QUEUE_H
6
7 #include "list.h"
8
9 template< class QUEUETYPE >
10 class Queue: private List< QUEUETYPE > {
11 public:
12 void enqueue( const QUEUETYPE &d ) { insertAtBack( d ); }
13 bool dequeue( QUEUETYPE &d )
14 { return removeFromFront( d ); }
15 bool isQueueEmpty() const { return isEmpty(); }
16 void printQueue() const { print(); }
17 };
18
19 #endif
20
21
22 // Fig. 15.12: fig15_12.cpp
23 // Driver to test the template Queue class
24 #include <iostream.h>
25 #include "queue.h"
26
27 int main()
28 {
29 Queue< int > intQueue;
30 int dequeueInteger;
31 cout << "processing an integer Queue" << endl;
32
33 for ( int i = 0; i < 4; i++ ) {
34 intQueue.enqueue( i );
35 intQueue.printQueue();
36 }
37
38 while ( !intQueue.isQueueEmpty() ) {
39 intQueue.dequeue( dequeueInteger );
40 cout << dequeueInteger << " dequeued" << endl;
41 intQueue.printQueue();
42 }
43
44 Queue< double > doubleQueue;
45 double val = 1.1, dequeuedouble;
46
47 cout << "processing a double Queue" << endl;
48
49 for ( i = 0; i < 4; i++ ) {
50 doubleQueue.enqueue( val );
51 doubleQueue.printQueue();
52 val += 1.1;
53 }
54
55 while ( !doubleQueue.isQueueEmpty() ) {
56 doubleQueue.dequeue( dequeuedouble );
57 cout << dequeuedouble << " dequeued" << endl;
58 doubleQueue.printQueue();
59 }
60
61 return 0;
62 }